package com.ruoyi.web.controller.thesis;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.YcThesis;
import com.ruoyi.domain.YcThesisScore;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IYcPersonalService;
import com.ruoyi.service.IYcThesisScoreService;
import com.ruoyi.service.IYcThesisService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 论文Controller
 *
 * @author 杨琛
 * @date 2020-09-09
 */
@Controller
@RequestMapping("/ruoyi/thesisAdmin")
public class YcThesisAdminController extends BaseController {
    private String prefix = "ruoyi/thesis";

    private final static String ALL="全部";

    @Autowired
    private IYcThesisService ycThesisService;

    @Resource
    private IYcThesisScoreService ycThesisScoreService;

    @Resource
    private IYcPersonalService ycPersonalService;

    @RequiresPermissions("ruoyi:thesis:thesisadmin")
    @GetMapping()
    public String thesis() {
        return prefix + "/thesisadmin";
    }

    /**
     * 查询论文列表
     */
    @RequiresPermissions("ruoyi:thesis:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YcThesis ycThesis) {
        startPage();
        //ALL代表全部,如果搜索框内是全部就给传一个空值
        if (ALL.equals(ycThesis.getJournalCategories())){
            ycThesis.setJournalCategories(null);
            ycThesis.setUserId(ShiroUtils.getUserId());
            List<YcThesis> list = ycThesisService.selectYcThesisListAdmin(ycThesis);
            return getDataTable(list);
        }
        //查询所有,因为管理员不需要看到所有,所以过滤出他需要看到的东西
        List<YcThesis> list = ycThesisService.selectYcThesisListAdmin(ycThesis);
        return getDataTable(list);
    }


    /**
     * 查询论文字典表
     *
     * @param ycThesisScore
     * @return
     */
    @GetMapping("/score")
    @ResponseBody
    public List<YcThesisScore> plans(YcThesisScore ycThesisScore) {
        //查询课题字典表所有的属性
        return ycThesisScoreService.selectYcThesisScoreList(ycThesisScore);

    }

    /**
     * 驳回备案
     */
    @RequiresPermissions("ruoyi:thesis:refusal")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/refusal")
    @ResponseBody
    public AjaxResult refusal(Long id) {
        //更新备案为驳回备案
        ycThesisService.refusal(id);
        return success();
    }


    /**
     * 通过备案
     */
    @RequiresPermissions("ruoyi:thesis:consent")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/consent")
    @ResponseBody
    public AjaxResult consent(Long id) {
        //更新备案状态为已备案
        ycThesisService.consent(id);
        return success();
    }


    /**
     * 通过审批
     */
    @RequiresPermissions("ruoyi:thesis:beApproved")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/beApproved")
    @ResponseBody
    public AjaxResult beApproved(Long id) {
        //更新审批状态为已通过
        ycThesisService.updateBeApproved(id);
        return success();
    }


    /**
     * 驳回审批
     */
    @RequiresPermissions("ruoyi:thesis:dismissTheApproval")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/dismissTheApproval")
    @ResponseBody
    public AjaxResult dismissTheApproval(Long id) {
        //更新审批状态为驳回
        ycThesisService.updateDismissTheApproval(id);
        return success();
    }


    /**
     * 查看详情人
     */
    @RequiresPermissions("ruoyi:thesis:detail")
    @GetMapping("/detail/{userId}")
    public String detail(@PathVariable("userId") Long userId, ModelMap mmap) {
        //通过前台传过来的userId,使用根据id查询查询出该id的对象出来,然后put到前台,前台进行解析
        mmap.put("list",ycPersonalService.selectYcPersonalById(userId));
        return prefix + "/personalInformation";
    }

}
